﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Super.DomainService;
using Super.Entities;
using Super.Infrastructure;

namespace Super.Web.Areas.Admin.Controllers
{
    public class DGOrderController : BaseController
    {
        /// <summary>
        /// 订购单查询 
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var adp = new DbContextAdapter();
            var dgorderService = new DGOrderService(adp);
            var page = Query.Params("pageNum");
            PageSize = 9;
            if (string.IsNullOrEmpty(page))
            {
                page = "1";
            }
           
            var keyword = Query.Post("keyword");

            var where = PredicateBuilder.True<DGOrder>();
            where = where.And(a => a.IsDel == false);
            if(!string.IsNullOrEmpty(keyword))
            {
                var id = keyword.ToInt(0);
                where = where.And(a=>a.ID==id||
                    a.ContactName.Contains(keyword)||
                    a.DGProduct.Title.Contains(keyword)||
                    a.OrderNo.Contains(keyword)
                    );

                ViewBag.keyword = keyword;
            }

        
            var dgorders = dgorderService.Repository.Find(where).OrderByDescending(a => a.ID).Page(page.ToInt(1), PageSize);
            BindPage(dgorders.ItemCount, page.ToInt(), PageSize);
            ViewBag.Records = dgorders.ItemCount;
            return View(dgorders.ItemCollection.ToList());
        }

        public ActionResult Print()
        {
            var id = Query.Get("id").ToInt();
            if(id>0)
            {
                var adp = new DbContextAdapter();
                var dgorderService = new DGOrderService(adp);
                var dgorderModel = dgorderService.Repository.Find(a => a.ID == id).FirstOrDefault();
                return View(dgorderModel);
            }
            return ErrorPage();
        }

    }
}
